package com.nf.health.app.customview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import com.nf.health.app.utils.DensityUtil;
import com.nf.health.app.utils.ScreentUtils;

@SuppressLint("DrawAllocation")
public class HpCircleGradeProgress extends View {

    private int width;
    private float radius;
    private float centerX;
    private float centerY;
    private RectF oval;
    private float max = 100;
    private float progress = 0;
    private Paint arcPaint;
    private Paint circlePaint;
    private Paint circleLinePaint;
    private Paint smallPaint;

    private int screenWidth;
    private int bkPaintWidth;

    private float smallX;
    private float smallY;

    public HpCircleGradeProgress(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public HpCircleGradeProgress(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public HpCircleGradeProgress(Context context) {
        super(context);
        init(context);
    }

    //24d6da
    //55dfbb
    private void init(Context context) {
        int[] colors = new int[]{Color.parseColor("#41b9fe"), Color.parseColor("#1fdfff")};//24d6da  55dfbb
        bkPaintWidth = DensityUtil.dip2px(context, 12);
        screenWidth = ScreentUtils.getScreenWidth(context);

        circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setColor(Color.parseColor("#fcc7a4"));//cd9866
        circlePaint.setStrokeWidth(bkPaintWidth);
        circlePaint.setStyle(Paint.Style.STROKE);

        circleLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        circleLinePaint.setColor(Color.parseColor("#E7E7E7"));
        circleLinePaint.setStrokeWidth(2);
        circleLinePaint.setStyle(Paint.Style.STROKE);

        LinearGradient shader = new LinearGradient(0, 0, screenWidth / 2, screenWidth / 2, colors, null,
                Shader.TileMode.MIRROR);
        arcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        arcPaint.setColor(Color.parseColor("#24d6da"));
        arcPaint.setStrokeWidth(bkPaintWidth);
        arcPaint.setStrokeCap(Paint.Cap.ROUND);
        arcPaint.setStyle(Paint.Style.STROKE);
        arcPaint.setShader(shader);

        smallPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        smallPaint.setColor(Color.WHITE);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        width = screenWidth / 2;
        centerX = width / 2;
        centerY = centerX;
        radius = centerX - bkPaintWidth / 2;
        oval = new RectF(centerX - radius, centerX - radius, centerX + radius,
                centerX + radius);
        setMeasuredDimension(width, width);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        initCirclePaint();
        canvas.drawCircle(centerX, centerY, radius, circlePaint);
        canvas.drawArc(oval, -90, 360 / max * progress, false, arcPaint);
        //确定小圆点的中心坐标
        smallX = (float) (centerX + radius * Math.sin(Math.toRadians(progress * 360 / max)));
        smallY = (float) (centerY - radius * Math.cos(Math.toRadians(progress * 360 / max)));
        //画小圆点
        canvas.drawCircle(smallX, smallY, (bkPaintWidth - 8) / 2, smallPaint);
    }


    public void setProgress(final float curProgress) {
        if (curProgress > max || curProgress < 0) {
            return;
        }
        progress = curProgress;
        postInvalidate(); //不建议再次开线程设置动画效果
    }

}
